#include "Sprite.h"

CSprite::CSprite(LPD3DXSPRITE spriteHandler, LPWSTR filePath, int spritePerRow, int countRow)
{
	D3DXIMAGE_INFO info;
	HRESULT result;

	mImage = NULL;
	mSpriteHandler = spriteHandler;

	result = D3DXGetImageInfoFromFile(filePath, &info);
	if (result != D3D_OK)
	{
		Trace(L"[ERROR] Failed to get information from image file '%s'", filePath);
		return;
	}

	mWidth = info.Width / spritePerRow;
	mHeight = info.Height / countRow;
	mCount = spritePerRow*countRow;
	mSpritePerRow = spritePerRow;
	mIndex = 0;

	LPDIRECT3DDEVICE9 d3ddv;

	mSpriteHandler->GetDevice(&d3ddv);

	result = D3DXCreateTextureFromFileEx(
		d3ddv,
		filePath,
		info.Width,
		info.Height,
		1,
		D3DUSAGE_DYNAMIC,
		D3DFMT_UNKNOWN,
		D3DPOOL_DEFAULT,
		D3DX_DEFAULT,
		D3DX_DEFAULT,
		D3DCOLOR_XRGB(255, 0, 255),
		&info,
		NULL,
		&mImage);

	if (result != D3D_OK)
	{
		Trace(L"[ERROR] Failed to create texture from file '%s'", filePath);
		return;
	}
}

void CSprite::Draw(D3DXVECTOR2 position, SpriteEffect spriteEffect, D3DXVECTOR2 scale, D3DCOLOR color, float rotation, float depth)
{
	this->Draw(NULL, position, spriteEffect, scale, color, rotation, depth);
}

void CSprite::Draw(LPDIRECT3DSURFACE9 target, D3DXVECTOR2 position, SpriteEffect spriteEffect, D3DXVECTOR2 scale, D3DCOLOR color, float rotation, float depth)
{
	mspriteRect = GetBound();
	D3DXVECTOR3 position3D;

	mCentrer = D3DXVECTOR2(mspriteRect.left + (float)mWidth / 2, mspriteRect.top + (float)mHeight / 2);

	////Scale sprite
	//D3DXMATRIX matrixScale;
	//position3D = D3DXVECTOR3(position.x / scale.x, position.y / scale.y, 0);
	//D3DXMatrixScaling(&matrixScale, scale.x, scale.y, 1);
	//mSpriteHandler->SetTransform(&matrixScale);

	//Flip & Scale sprite
	D3DXMATRIX oldMt;
	position3D = D3DXVECTOR3(position.x, position.y, 0);
	mSpriteHandler->GetTransform(&oldMt);
	//D3DXVECTOR2 scaling;
	switch (spriteEffect)
	{
	case SpriteEffect::FlipHorizontally:
		scale.x *= -1;
		break;
	case SpriteEffect::FlipVertically:
		scale.y *= -1;
		break;
	case SpriteEffect::FLipVerticallyAndHorizontally:
		scale.x *= -1;
		scale.y *= -1;
		break;
	}

	D3DXVECTOR2 scalingCenter(position3D.x + mWidth / 2, position3D.y + mHeight / 2);

	D3DXMATRIX newMt;
	D3DXMatrixTransformation2D(&newMt, &scalingCenter, rotation, &scale, NULL, 0.0f, NULL);
	D3DXMATRIX finalMt = newMt *oldMt;
	mSpriteHandler->SetTransform(&newMt);

	mSpriteHandler->Draw(
		mImage,
		&mspriteRect,
		NULL,
		&position3D,
		color == NULL ? D3DCOLOR_XRGB(255, 255, 255) : color);

	mSpriteHandler->SetTransform(&oldMt);
}

void CSprite::Next()
{
	mIndex = (mIndex + mCount - 1) % mCount;
}

void CSprite::Reset()
{
	mIndex = 0;
}

RECT CSprite::GetBound()
{
	RECT spriteRect;

	spriteRect.left = (mIndex % mSpritePerRow)*(mWidth)+1;
	spriteRect.top = (mIndex / mSpritePerRow)*(mHeight)+1;
	spriteRect.right = spriteRect.left + mWidth;
	spriteRect.bottom = spriteRect.top + mHeight + 1;

	return spriteRect;
}

CSprite::~CSprite()
{
	mImage->Release();
}
